package com.maomao.maomaochong_i.productPackage;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.maomao.maomaochong_i.R;
import com.maomao.maomaochong_i.chuangZhi.GuoWu;
import com.maomao.maomaochong_i.adapterPackage.GuoWuChe;
import com.maomao.maomaochong_i.chuangZhi.GuoWuShuJu;

/**
 * Created by maomao on 2017/5/17.
 */

public class PhotoPopupWindow extends PopupWindow implements View.OnClickListener{
    private Context context;
    private View view;
    private ImageView photoType;
    private TextView textView1, textView2, textView3;
    private TextView selectstr;
    private ImageButton imageButton;
    private Button color1, color2, color3, color4;
    private Button colour1, colour2;
    private Button capacity1, capacity2, capacity3;
    private Button version1;
    private Button combo1, combo2;
    private Button submit;
    private int price;
    private String allType = "请选择 机型";
    private String colorString = null, colourString = null, capacityString = null,
            comboString = null;
    private boolean opinionColor1 = false, opinionColor2 = false,opinionColor3 = false,
            opinionColor4 = false;
    private boolean opinionColour1 = false, opinionColour2 = false;
    private boolean opinionCapacity1 = false, opinionCapacity2 = false, opinionCapacity3 = false;
    private boolean opinionVersion1 = false;
    private boolean opinionCombo1 = false, opinionCombo2 = false;

    private int[] photoId = {R.mipmap.beauty01, R.mipmap.bea, R.mipmap.bea2, R.mipmap.bea3,
            R.mipmap.bea1};

    public PhotoPopupWindow(Context context, TextView selectstr){
        this.context = context;
        this.selectstr = selectstr;
        this.view = LayoutInflater.from(context).inflate(R.layout.product_select_buy, null);

        photoType = (ImageView) view.findViewById(R.id.productPhoto);
        textView1 = (TextView) view.findViewById(R.id.textView1);
        textView2 = (TextView) view.findViewById(R.id.textView2);
        textView3 = (TextView) view.findViewById(R.id.textView3);

        imageButton = (ImageButton) view.findViewById(R.id.productExit);

        color1 = (Button) view.findViewById(R.id.color1);
        color2 = (Button) view.findViewById(R.id.color2);
        color3 = (Button) view.findViewById(R.id.color3);
        color4 = (Button) view.findViewById(R.id.color4);

        colour1 = (Button) view.findViewById(R.id.colour1);
        colour2 = (Button) view.findViewById(R.id.colour2);

        capacity1 = (Button) view.findViewById(R.id.capacity1);
        capacity2 = (Button) view.findViewById(R.id.capacity2);
        capacity3 = (Button) view.findViewById(R.id.capacity3);

        version1 = (Button) view.findViewById(R.id.version1);

        combo1 = (Button) view.findViewById(R.id.combo1);
        combo2 = (Button) view.findViewById(R.id.combo2);

        submit = (Button) view.findViewById(R.id.submit);

        //监听
        imageButton.setOnClickListener(this);

        color1.setOnClickListener(this);
        color2.setOnClickListener(this);
        color3.setOnClickListener(this);
        color4.setOnClickListener(this);

        colour1.setOnClickListener(this);
        colour2.setOnClickListener(this);

        capacity1.setOnClickListener(this);
        capacity2.setOnClickListener(this);
        capacity3.setOnClickListener(this);

        version1.setOnClickListener(this);

        combo1.setOnClickListener(this);
        combo2.setOnClickListener(this);

        submit.setOnClickListener(this);

        //用方法
        someSet();

        photoType.setBackgroundResource(photoId[0]);
        textView3.setText(allType);
    }

    public View useView(){
        return view;
    }

    private void someSet(){
        // 设置外部可点击
        this.setOutsideTouchable(true);

        // 添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        this.view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int height = view.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();

                if (event.getAction() == MotionEvent.ACTION_UP){
                    if (y < height){
                        dismiss();
                    }
                }
                return true;
            }
        });

        //设置弹出窗口特征
        // 设置视图
        this.setContentView(this.view);

        // 设置弹出窗体的宽和高
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);

        // 设置弹出窗体可点击
        this.setFocusable(true);

        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x60000000);

        // 设置弹出窗体的背景
        this.setBackgroundDrawable(dw);

        // 设置弹出窗体显示时的动画，从底部向上弹出
        this.setAnimationStyle(R.style.take_photo_anim);
    }
    private GuoWu guoWu = GuoWu.getOne();
    private void tiaozhuan(){
        GuoWuChe guoWuChe = GuoWuChe.getOne(guoWu);
        GuoWuShuJu guoWuShuJu = new GuoWuShuJu();
        guoWuShuJu.setBiao(allType);
        guoWu.setShu(guoWuShuJu);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.submit:
                //修改传递过来的选购框的文本
                if (colorString != null && colourString != null && capacityString != null
                        && comboString != null) {
                    selectstr.setText(allType);

                    tiaozhuan();
                    dismiss();
                }
                else {
                    Toast.makeText(context,""+allType,Toast.LENGTH_SHORT).show();
                }


                break;

            case R.id.productExit:
                dismiss();
                break;

            case R.id.color1:
                if (opinionColor1 == false){
                    if (opinionColor2 == false && opinionColor3 == false
                            && opinionColor4 == false) {
                        opinionColor1 = true;
                        color1.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    else{
                        hideColor();
                        opinionColor1 = true;
                        color1.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    colorString = color1.getText().toString();
                    photoType.setBackgroundResource(photoId[1]);
                }
                else {
                    hideColor();
                    colorString = null;
                    photoType.setBackgroundResource(photoId[0]);
                }
                break;

            case R.id.color2:
                if (opinionColor2 == false){
                    if (opinionColor1 == false && opinionColor3 == false
                            && opinionColor4 == false) {
                        opinionColor2 = true;
                        color2.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    else{
                        hideColor();
                        opinionColor2 = true;
                        color2.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    colorString = color2.getText().toString();
                    photoType.setBackgroundResource(photoId[2]);
                }
                else {
                    hideColor();
                    colorString = null;
                    photoType.setBackgroundResource(photoId[0]);
                }
                break;

            case R.id.color3:
                if (opinionColor3 == false){
                    if (opinionColor1 == false && opinionColor2 == false
                            && opinionColor4 == false) {
                        opinionColor3 = true;
                        color3.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    else{
                        hideColor();
                        opinionColor3 = true;
                        color3.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    colorString = color3.getText().toString();
                    photoType.setBackgroundResource(photoId[3]);
                }
                else {
                    hideColor();
                    colorString = null;
                    photoType.setBackgroundResource(photoId[0]);
                }
                break;

            case R.id.color4:
                if (opinionColor4 == false){
                    if (opinionColor1 == false && opinionColor2 == false
                            && opinionColor3 == false) {
                        opinionColor4 = true;
                        color4.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    else{
                        hideColor();
                        opinionColor4 = true;
                        color4.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    colorString = color4.getText().toString();
                    photoType.setBackgroundResource(photoId[4]);
                }
                else {
                    hideColor();
                    colorString = null;
                    photoType.setBackgroundResource(photoId[0]);
                }
                break;

            case R.id.colour1:
                if (opinionColour1 == false){
                    if (opinionColour2 == false) {
                        opinionColour1 = true;
                        colour1.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    else{
                        hideColour();
                        opinionColour1 = true;
                        colour1.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    colourString = colour1.getText().toString();
                }
                else {
                    hideColour();
                    colourString = null;
                }
                break;

            case R.id.colour2:
                if (opinionColour2 == false){
                    if (opinionColour1 == false) {
                        opinionColour2 = true;
                        colour2.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    else{
                        hideColour();
                        opinionColour2 = true;
                        colour2.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    colourString = colour2.getText().toString();
                }
                else {
                    hideColour();
                    colourString = null;
                }
                break;

            case R.id.capacity1:
                if (opinionCapacity1 == false){
                    if (opinionCapacity2 == false && opinionCapacity3 == false) {
                        opinionCapacity1 = true;
                        capacity1.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    else{
                        hideCapacity();
                        opinionCapacity1 = true;
                        capacity1.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    capacityString = capacity1.getText().toString();
                }
                else {
                    hideCapacity();
                    capacityString = null;
                }
                break;

            case R.id.capacity2:
                if (opinionCapacity2 == false){
                    if (opinionCapacity1 == false && opinionCapacity3 == false) {
                        opinionCapacity2 = true;
                        capacity2.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    else{
                        hideCapacity();
                        opinionCapacity2 = true;
                        capacity2.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    capacityString = capacity2.getText().toString();
                }
                else {
                    hideCapacity();
                    capacityString = null;
                }
                break;

            case R.id.capacity3:
                if (opinionCapacity3 == false){
                    if (opinionCapacity1 == false && opinionCapacity2 == false) {
                        opinionCapacity3 = true;
                        capacity3.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    else{
                        hideCapacity();
                        opinionCapacity3 = true;
                        capacity3.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    capacityString = capacity3.getText().toString();
                }
                else {
                    hideCapacity();
                    capacityString = null;
                }
                break;

            case R.id.version1:
                if (opinionVersion1 == false){
                    opinionVersion1 = true;
                    version1.setBackgroundResource(R.drawable.pop_button_click);
                }
                else {
                    hideVersion();
                }
                break;

            case R.id.combo1:
                if (opinionCombo1 == false){
                    if (opinionCombo2 == false) {
                        opinionCombo1 = true;
                        combo1.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    else{
                        hideCombo();
                        opinionCombo1 = true;
                        combo1.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    comboString = combo1.getText().toString();
                }
                else {
                    hideCombo();
                    comboString = null;
                }
                break;

            case R.id.combo2:
                if (opinionCombo2 == false){
                    if (opinionCombo1 == false) {
                        opinionCombo2 = true;
                        combo2.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    else{
                        hideCombo();
                        opinionCombo2 = true;
                        combo2.setBackgroundResource(R.drawable.pop_button_click);
                    }
                    comboString = combo2.getText().toString();
                }
                else {
                    hideCombo();
                    comboString = null;
                }
                break;
        }

        if (opinionColour1 == true){
            if (opinionCapacity1 == true){
                price = 16*9;
            }
            else if (opinionCapacity2 == true){
                price = 64*9;
            }
            else if (opinionCapacity3 == true){
                price = 128*9;
            }
            else {
                price = 16*9;
            }
        }
        else if(opinionColour2 == true){
            if (opinionCapacity1 == true){
                price =(int) (16 * 9.5);
            }
            else if (opinionCapacity2 == true){
                price =(int) (64 * 9.5);
            }
            else if (opinionCapacity3 == true){
                price =(int) (128 * 9.5);
            }
            else {
                price =(int) (16 * 9.5);
            }
        }
        else {
            price = (int) (16 * 9);
        }

        if (colorString == null){
            allType = "请选择 颜色";
        }
        else if (colourString == null){
            allType = "请选择 成色";
        }
        else if (capacityString == null){
            allType = "请选择 容量";
        }
        else if (comboString == null){
            allType = "请选择 套餐";
        }
        else if(colorString != null && colourString != null && capacityString != null
                && comboString != null){
            allType = "已选择:  \"" + colorString + "\"、\"" + colourString
                    + "\"、\"" + capacityString + "\"、\"" + comboString + "\"";
        }
        textView1.setText("￥"+price);
        textView3.setText(allType);

    }

    private void hideColor(){
        color1.setBackgroundResource(R.drawable.pop_button_noclick);
        color2.setBackgroundResource(R.drawable.pop_button_noclick);
        color3.setBackgroundResource(R.drawable.pop_button_noclick);
        color4.setBackgroundResource(R.drawable.pop_button_noclick);
        opinionColor1 = false;
        opinionColor2 = false;
        opinionColor3 = false;
        opinionColor4 = false;
    }

    private void hideColour(){
        colour1.setBackgroundResource(R.drawable.pop_button_noclick);
        colour2.setBackgroundResource(R.drawable.pop_button_noclick);
        opinionColour1 = false;
        opinionColour2 = false;
    }

    private void hideCapacity(){
        capacity1.setBackgroundResource(R.drawable.pop_button_noclick);
        capacity2.setBackgroundResource(R.drawable.pop_button_noclick);
        capacity3.setBackgroundResource(R.drawable.pop_button_noclick);
        opinionCapacity1 = false;
        opinionCapacity2 = false;
        opinionCapacity3 = false;
    }

    private void hideVersion(){
        version1.setBackgroundResource(R.drawable.pop_button_noclick);
        opinionVersion1 = false;
    }

    private void hideCombo(){
        combo1.setBackgroundResource(R.drawable.pop_button_noclick);
        combo2.setBackgroundResource(R.drawable.pop_button_noclick);
        opinionCombo1 = false;
        opinionCombo2 = false;
    }
}

